﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.VisualStudio.TextTemplating;
using System.CodeDom.Compiler;

namespace Bing.Toolkit.T4
{
    public static class T4Engine
    {
        public static string BuilderText(string strTemplateContent, T4TemplatingEngineHost host, out string strErrorMessage)
        {
            string text = new Engine().ProcessTemplate(strTemplateContent, host);
            StringBuilder sbError = new StringBuilder();
            foreach (CompilerError error in host.Errors)
            {
                sbError.AppendFormat("Number:{0} ", error.ErrorNumber);
                sbError.AppendFormat("Line:{0} ", error.Line);
                sbError.AppendFormat("Path:{0}\r\n", error.FileName);
                sbError.Append(error.ErrorText);
                sbError.Append("\r\n\r\n");
            }
            strErrorMessage = sbError.ToString();
            return text;
        }

        public static string BuilderTextWithFileName(string fileName, T4TemplatingEngineHost host, out string strErrorMessage)
        {
            return BuilderText(File.ReadAllText(fileName), host, out strErrorMessage);
        }
    }
}
